package com.allprs;

import com.allprs.common.utils.ip.IpUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

/**
 * 启动程序
 *
 * @author wjf
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class SingleBootApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SingleBootApplication.class, args);
        printKeyLoadMessage(run);
    }

    /**
     * 获取Key加载信息
     */
    public static boolean printKeyLoadMessage(ConfigurableApplicationContext context) {
        ConfigurableEnvironment environment = context.getEnvironment();
        // 项目端口
        String port = environment.getProperty("server.port");
        // 项目路径
        String contextPath = environment.getProperty("server.servlet.context-path");
        // 项目名称
        // String projectFinalName = environment.getProperty("server.servlet.context-path");

        String homeUrl = "http://" + IpUtils.getHostIp() + ":" + port + contextPath;
        String swaggerUrl = "http://" + IpUtils.getHostIp() + ":" + port + contextPath + "doc.html";
        StringBuilder sb = new StringBuilder();
        sb.append("\r\n======================================================================\r\n");
        sb.append("\r\n======================================================================\r\n");
        System.out.println(sb.toString());
        System.out.println("\t前端页面本地访问地址:    " + homeUrl);
        System.out.println("\tswagger本地访问地址: " + swaggerUrl);
        System.out.println("\r");
        return true;
    }

    public static void printInitBean(ConfigurableApplicationContext context) {
        // 打印所有的Bean
        // String[] beanNames = context.getBeanDefinitionNames();
        // 打印所有添加该注解的bean
        String[] beanNames = context.getBeanNamesForAnnotation(Component.class);
        System.out.println("bean总数:{}" + context.getBeanDefinitionCount());
        int i = 0;
        for (String str : beanNames) {
            System.out.println("{},beanName:{}" + (++i) + str);
        }
    }

}
